import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import * as _ from 'lodash';
import { AuthService } from '../../core/auth/auth.service';

@Component({
    selector: 'app-portal',
    templateUrl: './portal.component.html',
    styleUrls: ['./portal.component.scss']
})
export class PortalComponent implements OnInit {
    manageName = '跃进渠';
    remarks = '';

    gateSubMenus = [];
    wsSubMenus = [];
    pmSubMenus = [];
    sfSubMenus = [];

    linkList = [
        {
            href: 'http://172.30.64.140:4061/nxszy/login.html',
            name: '国控水资源'
        },
        {
            href: 'http://172.30.134.242:9008/api/index',
            name: '宁夏水资源'
        },
        {
            href: 'http://slt.nx.gov.cn/',
            name: '宁夏回族自治区水利厅'
        },
    ]
    constructor(
        private router: Router,
        private authService: AuthService
    ) {
    }

    ngOnInit(): void {
        this.manageName = JSON.parse(localStorage.getItem('userInfo'))['manageName'];
        this.remarks = JSON.parse(localStorage.getItem('roleInfo'))['remarks'];
        const gateMenuList = JSON.parse(localStorage.getItem('gateMenuList'));
        const wsMenuList = JSON.parse(localStorage.getItem('wsMenuList'));
        const pmMenuList = JSON.parse(localStorage.getItem('pmMenuList'));
        const sfMenuList = JSON.parse(localStorage.getItem('sfMenuList'));
        this.gateSubMenus = this.getSubMenus(gateMenuList);
        this.wsSubMenus = this.getSubMenus(wsMenuList);
        this.pmSubMenus = this.getSubMenus(pmMenuList);
        this.sfSubMenus = this.getSubMenus(sfMenuList);
    }

    getSubMenus(list): Array<any> {
        const resultList = [];
        list.forEach(menu => {
            if (menu.parentId === '0' && !!menu.href) {
                resultList.push(menu);
            } else if ((!menu.href || !menu.href.trim()) && menu.parentIds.split(',').length < 2) {
                const pid = menu.id;
                const child = list.find(sub => {
                    return sub.parentIds.indexOf(pid) >= 0;
                });
                const result = {
                    ...menu
                };
                if (child.href) {
                    result.href = child.href;
                } else {
                    const subChild = child.children[0];
                    result.href = subChild.href;
                }
                resultList.push(result);
            }
        });
        return resultList;
    }
    navigateToSys(url = 'map'): void {
        let path = '/map';
        let menu = 'mapMenuList';
        switch (url) {
            case 'map':
                path = '/map';
                menu = 'mapMenuList';
                break;
            case 'wrs':
                path = '/realtime-control/dispatch-glc';
                menu = 'wsMenuList';
                break;
            case 'gate':
                path = '/gate/records';
                menu = 'gateMenuList';
                break;
            case 'pm':
                path = '/pm/file-mng';
                menu = 'pmMenuList';
                break;
            case 'sf':
                path = '/sf/prod-mng';
                menu = 'sfMenuList';
                break;
        }
        const currentMenuList = JSON.parse(localStorage.getItem(menu));
        this.authService.updateActiveTopMenu(_.find(currentMenuList, item => {
            return item.href === path;
        }));
        this.router.navigate([path]).then(() => {
            this.authService.updateMenuData(currentMenuList);
        });
    }

    navigateToSubMenu(href, menu) {
        const currentMenuList = JSON.parse(localStorage.getItem(menu));
        this.authService.updateActiveTopMenu(_.find(currentMenuList, item => {
            return item.href === href;
        }));
        this.router.navigate([href]).then(() => {
            this.authService.updateMenuData(currentMenuList);
        });
    }

    jumpToLink(url) {
        window.open(url);
    }
}
